package com.lzb.newloan;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.utils.AlertDialog;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.Formatdou;
import com.lzb.newloan.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 投资的各种列表
 */
public class InvestOutMoneyFragment extends Fragment implements OnClickListener {

	private View view;
	private Activity ac;
	private TextView out_money_number_txt,out_money_value_number_txt;
	private TextView income;
	private String userId;
	private TextView text1,text5;
	private TextView text2;
	private TextView text3;

	private int overduePen,pendingRepayment,repaymentAmount;
	private LinearLayout curent_mon;

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		if (view==null) {
			view = inflater.inflate(R.layout.fragment_invest_out_money, null);
			initView();
			initData();
		}else {
			((ViewGroup)view.getParent()).removeView(view);
		}
		return view;
	}

	private void initView() {
		ac = getActivity();
		SharedPreferences sp = ac.getSharedPreferences("config", 0x0000);
		userId = sp.getString("userid", "");
		LinearLayout my_money_linear = (LinearLayout) view.findViewById(R.id.my_money_linear);		
		my_money_linear.setOnClickListener(this);
		LinearLayout fking = (LinearLayout) view.findViewById(R.id.fking);
		fking.setOnClickListener(this);
		LinearLayout wait_fking = (LinearLayout) view.findViewById(R.id.wait_fking);
		wait_fking.setOnClickListener(this);
		LinearLayout old_fking = (LinearLayout) view.findViewById(R.id.old_fking);
		old_fking.setOnClickListener(this);
		LinearLayout old_money = (LinearLayout) view.findViewById(R.id.old_money);
		old_money.setOnClickListener(this);
		LinearLayout cuishou_linear = (LinearLayout) view.findViewById(R.id.cuishou_linear);
		cuishou_linear.setOnClickListener(this);
        view.findViewById(R.id.invest_money_linear).setOnClickListener(this);
		out_money_number_txt = (TextView) view.findViewById(R.id.out_money_number_txt);
		income = (TextView) view.findViewById(R.id.income);
		text1 = (TextView) view.findViewById(R.id.text1);
		text2 = (TextView) view.findViewById(R.id.text2);
		text3 = (TextView) view.findViewById(R.id.text3);
		text5 = (TextView)view.findViewById(R.id.text5);
		curent_mon = (LinearLayout) view.findViewById(R.id.curent_mon);

		out_money_value_number_txt = (TextView) view.findViewById(R.id.out_money_value_number_txt);
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case Config.CODE_INVESTMONEY:
					String reString = msg.obj.toString();
					try {
						JSONObject response = new JSONObject(reString);
						Double  totalIncome = response.optDouble("totalIncome");//投资总收益
						income.setText("￥"+ Formatdou.formatdou(totalIncome));
						int investmentPen = response.optInt("investmentPen");//投资总笔数
						out_money_number_txt.setText(investmentPen+"");
						 pendingRepayment = response.optInt("pendingRepayment");//待还款笔数
						String pendingRepaymentTotalMoney = response.optString("pendingRepaymentTotalMoney");//待还款金额
						text1.setText(pendingRepaymentTotalMoney+"元，"+pendingRepayment+"笔");
						 repaymentAmount = response.optInt("repaymentAmount");//已还款笔数
						String repaymentTotalAmount = response.optString("repaymentTotalAmount");//已还款金额
						text2.setText(repaymentTotalAmount+"元，"+repaymentAmount+"笔");
						overduePen = response.getInt("overduePen");//已逾期笔数
						String overTotalMoney = response.getString("overTotalMoney");//已逾期总金额
						text3.setText(overTotalMoney+"元，"+overduePen+"笔");
						String generalAudit = response.getString("generalAudit");//投资总额
						out_money_value_number_txt.setText(generalAudit);
						String money = response.optString("investUsableSum");
						if (TextUtils.isEmpty(money)){
							curent_mon.setVisibility(View.GONE);
						}else{
							double m = Double.parseDouble(money);
							if (m<0){
								curent_mon.setVisibility(View.GONE);
							}else{
								curent_mon.setVisibility(View.VISIBLE);
								text5.setText(money+"元");
							}

						}
					} catch (JSONException e1) {
						e1.printStackTrace();
					}

					break;
				case Config.CODE_URL_ERROR:
					Toast.makeText(ac, "url错误",
							Toast.LENGTH_SHORT).show();
					break;
				case Config.CODE_NET_ERROR:
					Toast.makeText(ac, "网络错误",
							Toast.LENGTH_SHORT).show();
					// if (time!=null) {
					// time.cancel();
					// }
					break;

				default:
					break;
			}
		};
	};

	private void initData() {
		HttpUtils.doPostAsyn(Config.INVESTMONEY_CORD + "&userid="
				+ userId, "",mHandler, Config.CODE_INVESTMONEY);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
            case R.id.invest_money_linear://投资记录
                startActivity(new Intent(ac, com.lzb.newloan.InvestRecordActivity.class));
                break;
		case R.id.my_money_linear://我的财富
			startActivity(new Intent(ac, MyMoneyActivity.class));
			break;
		case R.id.cuishou_linear://催收
			startActivity(new Intent(ac, MyOverDueListActivity.class));
			break;
		case R.id.fking://正放款
			Intent waiting = new Intent(getActivity(), WaitLoanActivity.class);
			waiting.putExtra("type", "1");
			startActivity(waiting);
			break;
		case R.id.wait_fking://待还款
			if(pendingRepayment!=0){
				Intent wait = new Intent(getActivity(), WaitLoanActivity.class);
				wait.putExtra("type", "2");
				startActivity(wait);
			}else {
				showDialog("您当前没有正在还款的借款用户");
			}
			break;
		case R.id.old_fking://已还款
			if(repaymentAmount!=0){
				Intent waited = new Intent(getActivity(), WaitLoanActivity.class);
				waited.putExtra("type", "3");
				startActivity(waited);
			}else {
				showDialog("您当前没有已还款的借款用户");
			}
			break;
		case R.id.old_money://已逾期
			if(overduePen!=0){
				Intent waited = new Intent(getActivity(), WaitLoanActivity.class);
				waited.putExtra("type", "4");
				startActivity(waited);
			}else {
				showDialog("您当前没有已逾期的借款用户");
			}

			break;
		default:
			break;
		}
	}
	
	
	
	private void showDialog(String message) {
		new AlertDialog(
				ac).builder().setMsg(message)
				.setNegativeButton("确定", new OnClickListener() {
					@Override
					public void onClick(View v) {
					}
				}).show();
	}
}
